package com.lovo.project.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lovo.project.bean.WarehouseBean;
import com.lovo.project.service.WarehouseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("/goods")
public class WarehouseController {

    @Autowired
    WarehouseService warehouseService;

    @RequestMapping("/queryGoodsList")
    public Page<WarehouseBean> queryGoodsList(Integer pg, Integer pgsize, String goodsName, Integer startNumber, Integer endNumber, String goodsType
            , Integer salePrice) {
        QueryWrapper queryWrapper = new QueryWrapper();
        if (goodsName != null && goodsName != "") {
            queryWrapper.like("goods_name", goodsName);
        }
        if (startNumber!= null) {
            queryWrapper.ge("goods_number", startNumber);
        }
        if (endNumber != null) {
            queryWrapper.le("goods_number", endNumber);
        }
        if (salePrice == null) {
            queryWrapper.gt("goods_sale_price", 0);
        } else if (salePrice == 0) {
            queryWrapper.eq("goods_sale_price", 0);
        }
        if (goodsType != null) {
            queryWrapper.eq("goods_type", goodsType);
        }
        Page<WarehouseBean> page = new Page<>(pg, pgsize);
        Page<WarehouseBean> pageInfo = warehouseService.queryGoodsList(page, queryWrapper);
        return pageInfo;
    }

    @RequestMapping("/queryGoods")
    public WarehouseBean queryGoods(Integer goodsId) {
        WarehouseBean warehouseBean = warehouseService.queryWarehouse(goodsId);
        return warehouseBean;
    }

    @RequestMapping("/updateGoods")
    public String updateGoods(Integer goodsId, double goodsSalePrice) {
        WarehouseBean warehouseBean = new WarehouseBean();
        warehouseBean.setGoodsId(goodsId);
        warehouseBean.setGoodsSalePrice(goodsSalePrice);
        warehouseService.updateGoodsPrice(warehouseBean);
        return "ok";
    }
}
